# -*- coding: utf-8 -*-
import os
import sys
import time

reload(sys)
sys.setdefaultencoding("utf-8")


class CommonUtils(object):
    """ 工具类 """

    def __init__(self, logger):
        self.log = logger

    # 执行本地shell命令，返回状态码和结果
    def exec_local_shell(self, script):
        self.log.info("执行语句: " + script)
        p = os.popen(script)
        out = p.readlines()
        return out, p.close()

    # 执行shell命令，返回状态码
    def exec_local_shell0(self, script):
        self.log.info("执行语句: " + script)
        stat_code = os.system(script)
        return stat_code

    # 循环等待策略
    def sleep_strategy(self, loop, sleep_sec, message):
        self.log.info("{}, 循环 {}, 等待时间: {}".format(message, loop, sleep_sec))
        time.sleep(int(sleep_sec))

    def is_part_sync(self, tab_size, tab_unit, cls_full_limit, cls_full_limit_unit):
        tab_size_kb = float(tab_size)
        if tab_unit == "T":
            tab_size_kb = float(tab_size) * 1000000000
        elif tab_unit == "G":
            tab_size_kb = float(tab_size) * 1000000
        elif tab_unit == "M":
            tab_size_kb = float(tab_size) * 1000

        limit_kb = float(cls_full_limit)
        if cls_full_limit_unit == "T":
            limit_kb = float(cls_full_limit) * 1000000000
        elif cls_full_limit_unit == "G":
            limit_kb = float(cls_full_limit) * 1000000
        elif cls_full_limit_unit == "M":
            limit_kb = float(cls_full_limit) * 1000

        if tab_size_kb > limit_kb:
            return True
        return False

    # 通过开始日期和结束日期过滤分区日期
    def filter_by_part_dt(self, part_name, part_start_dt, part_end_dt):
        dt = part_name.split("=")[-1]
        if part_start_dt == "-" and part_end_dt == "-":
            return False
        elif part_start_dt == "-" and part_end_dt != "-":
            if dt <= part_end_dt:
                return False
            return True
        elif part_start_dt != "-" and part_end_dt == "-":
            if dt < part_start_dt:
                return True
            return False
        else:
            if part_start_dt <= dt <= part_end_dt:
                return False
            return True

    def date_format(self, fmt='%Y-%m-%d'):
        times = time.time()
        fmt_time = time.strftime(fmt, time.localtime(times))
        return fmt_time


if __name__ == '__main__':
    util = CommonUtils(None)
    print(util.is_part_sync(11, "G", 10001, "M"))
